<?php

/*
* the listener is another back end who subscribe to get notified when a particular tag appears in a status
*/

class Status_Object_Listener {
	
	protected $_module = '';
	protected $_controller = '';
	protected $_action = '';
	protected $_tag = '';
		
	/* 
	definition of the tagscounter  table 
	this table contains the number of times each tag is used by people
	*/  
	
	static protected $_dbListenersTableName = 'listeners';
	static protected $_dbListenersModuleCol = 'module';
	static protected $_dbListenersControllerCol = 'controller';
	static protected $_dbListenersActionCol = 'action';
	static protected $_dbListenersTagCol = 'tag';

  
	/*
	__construct - public constructor of the class. It is defined by the type of timeline that is requested and the identifier of the timeline
	*/
	public function __construct($data) {
		$this->_module = $data[self::$_dbListenersModuleCol];
		$this->_controller = $data[self::$_dbListenersControllerCol];
		$this->_action = $data[self::$_dbListenersActionCol];
		$this->_tag = $data[self::$_dbListenersTagCol];
		
	}	//end constructor
	
	
	public function getModule() {return $this->_module;}
	public function getController() {return $this->_controller;}
	public function getAction() {return $this->_action;}
	public function getTag() {return $this->_tag;} 
	
	static public function getListeners($oStatus) {
		//basic data check
		if(!($oStatus instanceof Status_Object_Status)) {
			require_once('status/objects/Exception.php');
			throw new Status_Object_Exception('invalid status',60);
		}
			
		//We retrieve the list of tags
		$tags = $oStatus->getTags();
		if(empty($tags)) return array();
		
		//we get the tag counter database
		$listenersList = array();
		$db = self::_getListenersDbStatic();
		foreach($tags as $num => $tag) {
			//we count the current amount of tags
			$select = $db->select()->from(self::$_dbListenersTableName)->where($db->quoteInto(self::$_dbListenersTagCol.' = ?',$tag));
			$res = $db->fetchAll($select->__toString());    	  
	
	
			//if the tag doesn t exists we create it
			if(empty($res)) {
				continue;
			} else {
				foreach($res as $num => $data) {
					$listenersList[] = new Status_Object_Listener($data);
				} //end listener not empty
			} //end else tag exists
			
		} //end foreach tag	
		
		
		return $listenersList;
		
	}
	static public function addListener($module,$controller,$action,$tag) {
		//We should have a way to check that the module controller action really exists, let s trust them for the time being
		//we just validate the syntax
		if(!self::_isValidName($module) || !self::_isValidName($controller) || !self::_isValidName($action) || !self::_isValidTag($tag)) {
			require_once('apidefinition/objects/Exception.php');
			throw new Status_Object_Exception('Mandatory data not OK',3);				
		}
		
		$db = self::_getListenersDbStatic();
		
		//check that it doesn t already exists
		$select = $db->select()->from(self::$_dbListenersTableName);
		$select->where($db->quoteInto(self::$_dbListenersModuleCol.' = ?',$module));
		$select->where($db->quoteInto(self::$_dbListenersControllerCol.' = ?',$controller));
		$select->where($db->quoteInto(self::$_dbListenersActionCol.' = ?',$action));
		$select->where($db->quoteInto(self::$_dbListenersTagCol.' = ?',$tag));
		
		$res = $db->fetchAll($select->__toString());
		
		if(!empty($res)) {
			require_once('status/objects/Exception.php');
			throw new Status_Object_Exception('Listener is already existing',72);			
		}
		
		$data = array( self::$_dbListenersModuleCol => $module,
			self::$_dbListenersControllerCol => $controller,
			self::$_dbListenersActionCol => $action,
			self::$_dbListenersTagCol =>  $tag );
			
		$db->insert(self::$_dbListenersTableName,$data);
		
		return;
		
	} 
	
	static protected function _getListenersDbStatic() {
		return Zend_Registry::get('dt_dbs')->getDb('status','listeners','key');	
	} //end _getTagsCounterDbStatic

  
	static protected function _isValidTag($tag) {
		if(preg_match(  '/([^a-zA-z0-9_]+)/i'  ,  $tag)) return false;
		if(empty($tag)) return false;
		return true;
	} //end _isValidTag
	
	static protected function _isValidName($name) {
	
		if(empty($name)) {
			require_once('status/objects/Exception.php');
			throw new Status_Object_Exception('Mandatory data is missing',1);
		}
		
		//Controller and module name can only be lowercase alphabet characters and less or equal 32 characters
		if(preg_match('/[^a-z]/',$name)) {
			require_once('apidefinition/objects/Exception.php');
			throw new Status_Object_Exception('Controller or module name can only be alphabet lower case characters',2);  		
		}
		if(32 < strlen($name)) {
			require_once('apidefinition/objects/Exception.php');
			throw new Status_Object_Exception('Controller or module name must be less than 33 characters',3);
		}
		return true;	
	} //end is valid name
  
} //end class